#include <iostream>
#include "evnetplus/udp/udp_server.h"
#include "evnetplus/event_loop.h"
#include "evnetplus/event_loop_thread_pool.h"

using namespace evnetplus;

int main(int argc, char const *argv[])
{
    udp::Server::Ptr server = udp::Server::Ptr(new udp::Server);
    server->SetThreadDispatchPolicy(ThreadDispatchPolicy::kIPAddressHashing);
    server->SetMessageHandler([](EventLoop* loop, udp::MessagePtr& msg) {
        std::cout << "func=" << __FUNCTION__ << " OK"
            << " body=" << std::string(msg->data(), msg->size()) << "\n";
        udp::SendMessage(msg);
    });
    server->Init(11450);
    server->Start();

    EventLoopThread baseloopthread;
    baseloopthread.Start(true);
    EventLoopThreadPool::Ptr loopthreadpool = EventLoopThreadPool::Ptr(new EventLoopThreadPool(baseloopthread.loop(), 4));
    server->SetEventLoopThreadPool(loopthreadpool);
    loopthreadpool->Start(true);


    bool bEnableStop = false;
    SignalEventWatcher signalWatcher(SIGINT, baseloopthread.loop(), [&bEnableStop](){
        std::cout << "atched signal" << std::endl;
        bEnableStop = true;
    });
    signalWatcher.Init();
    signalWatcher.AsyncWait();

    while (!bEnableStop) {
        std::this_thread::sleep_for(std::chrono::milliseconds(500));
    }

    server->Stop(true);
    loopthreadpool->Stop();
    baseloopthread.Stop(true);

    return 0;
}
